﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo._7C;
using ConfExpert.Properties;
using ConfExpert.Utility;

using DevExpress.Utils;
using DevTeam;

using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.SatinAlma
{
    public partial class EnAzIlave : XForm
    {
        int CKBatchId;
        public EnAzIlave(int _CKBatchId)
        {
            InitializeComponent();
            CKBatchId = _CKBatchId;
            this.Text += "- " + AppSession.Lookups.CKBatch.FindByCKBatchId(CKBatchId).Notes;
        }

        private void EnAzIlave_Load(object sender, EventArgs e)
        {
            

            DataSet d = Session.DataManager.ExecuteDataSet("GetAlg2", CKBatchId);
            gridView1.Init(d.Tables[1]);
            gridView2.Init(d.Tables[0]);
            //gridView3.SutunEkle("QtyRequired", typeof(decimal), "ISNULL(QtyNeeded,0)-ISNULL(QtyStok,0)");   //TODO: çalışmadı!

            gridView2.FormatAsNumber(0, "Adet");
            gridView2.AddSummary("Adet");

            gridView1.FormatAsDate("ETAVarisTarihi", "GerDepoVarisTarihi");
            gridView1.FormatAsNumber(1, "QtyNeeded", "QtyStok", "QtyRequired", "Mt", "YuklenenMt");
            gridView1.AddSummary("QtyNeeded", "QtyStok", "QtyRequired", "Mt", "YuklenenMt");

            gridView1.RowCellStyle += gridView2_RowCellStyle;
            gridView1.ActiveFilterString = "[EksikFazla] = 'E'";

            gridView1.BestFitColumns();
            gridView2.BestFitColumns();

            splitContainerControl2.AutoSize();

            if (ytk(80))
            {
                //gm.GridMenu.AddMenu("Topların Yerini Göster", ToplarinYeriniGoster);
                //gm.GridMenu.AddMenu("Kapat", Kapat);
                GridManager gm = GridManager.GetManager(gridView1);
                gm.GridMenu.AddMenu("Bu eksikleri discard ederek hesabı yenile", Discard, Resources.ok2);
            }

            Art = new DataTable("Art");
            DataColumn colP = new DataColumn("ProductId", typeof(int));
            DataColumn colQ = new DataColumn("Qty", typeof(int));
            Art.Columns.AddRange(new DataColumn[] { colP, colQ });
        }

        void Discard(object sender, EventArgs e)
        {
            Art.Clear();
            foreach (int rowHandle in gridView1.GetSelectedRows())
                if (rowHandle > -1)
                {
                    int ProductId = intParse(gridView1.GetRowCellValue(rowHandle, "ProductId"));
                    decimal QtyRequired = decimalParse(gridView1.GetRowCellValue(rowHandle, "QtyRequired")) + 0.5M;
                    if (ProductId > 0)
                        Art.Rows.Add(ProductId, QtyRequired);
                }

            AppSession.MainForm.ShowMdiChildForm(typeof(Dikilebilecek), CKBatchId, Art);
        }


        DataTable Art;
        void gridView2_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "QtyRequired")
                if (decimalParse(e.CellValue) > 0)
                    e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);

        }
    }
}